package cnsatmsmart.automation.mmi.controller.event;

import com.bbn.openmap.LatLonPoint;
import com.bbn.openmap.MapBean;
import com.bbn.openmap.event.NavMouseMode2;
import com.bbn.openmap.proj.Proj;
import com.bbn.openmap.proj.ProjMath;
import com.bbn.openmap.proj.Projection;
import com.bbn.openmap.util.Debug;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class OverviewMouseMode extends NavMouseMode2{

    /* atribut - atribut yang dimiliki oleh kelas OverviewMouseMode */
        private MapBean MapPanel;
        private JDialog ZoomAreaWindow;
        private JTabbedPane tabPanel;
        private boolean isNewWindow;
        private JPanel panel2;

    /**
     * Constructor - user defined constructor, no defaut constructor
     * @param MapPanel panel peta
     * @param zoomAreaDia dialog dari zoom area
     * @param tabPanel panel tab
     * @param isNewWindow true jika akan membuat jendela dialog baru dan false jika membuat tab baru
     */
        public OverviewMouseMode(MapBean MapPanel, JDialog zoomAreaDia, JTabbedPane tabPanel, boolean isNewWindow)
        {
            super(true);
            this.MapPanel= MapPanel;
            this.ZoomAreaWindow = zoomAreaDia;
            this.tabPanel = tabPanel;
            this.isNewWindow = isNewWindow;
        }

    /* method - method yang dimiliki oleh kelas OverviewMouseMode */
        @Override
        public void mouseReleased(MouseEvent e)
        {
            //menghapus zoomArea yang ada
            if (isNewWindow)
            {
                if (MapPanel != null)
                    ZoomAreaWindow.remove(MapPanel);
            }
            else
                this.tabPanel.remove(panel2);


            if (Debug.debugging("mousemode")) {
                System.out.println(getID() + "|DefaultOverviewMouseMode.mouseReleased()");
            }

            Object obj = e.getSource();
            if (!this.mouseSupport.fireMapMouseReleased(e)) {
                if ((!(obj instanceof MapBean)) || (!this.autoZoom) || (this.point1 == null))
                return;
                MapBean map = (MapBean)obj;
                Projection projection = map.getProjection();
                Proj p = (Proj)projection;

                synchronized (this) {
                    this.point2 = e.getPoint();
                    int dx = Math.abs(this.point2.x - this.point1.x);
                    int dy = Math.abs(this.point2.y - this.point1.y);

                    if ((dx < 5) || (dy < 5))
                    {
                        paintRectangle(map, this.point1, this.point2);

                        if ((dx < 5) && (dy < 5)) {
                            LatLonPoint llp = projection.inverse(e.getPoint());
                            this.MapPanel.setCenter(llp);
                        }

                        //karena terlalu kecil, maka tidak akan ditampilkan zoom areanya dan panel atau dialog yang muncul ditutup
                        if (this.ZoomAreaWindow.isVisible())
                            this.ZoomAreaWindow.setVisible(false);
                        else
                            if (this.tabPanel.getTabCount() != 1)
                                this.tabPanel.remove(1);

                        JOptionPane.showMessageDialog(tabPanel, "Area too small.");
                        return;
                    }
                    dx = Math.abs(this.point2.x - this.point1.x);
                    dy = Math.abs(this.point2.y - this.point1.y);

                    Point cornerPoint1 = new Point(this.point2.x < this.point1.x ? this.point2.x : this.point1.x, this.point2.y < this.point1.y ? this.point2.y : this.point1.y);

                    Point cornerPoint2 = new Point(cornerPoint1.x + 2 * dx, cornerPoint1.y + 2 * dy);

                    float newScale = ProjMath.getScale(cornerPoint1, cornerPoint2, projection);

                    LatLonPoint center = projection.inverse(this.point1.x, this.point1.y);

                    this.MapPanel.setScale(newScale);
                    this.MapPanel.setCenter(center);
                }

                this.point1 = null;
                this.point2 = null;
            }

            if (isNewWindow) //jika ingin membuat pada dialog baru
            {
                //membuat zoomArea pada dialog yang disediakan
                ZoomAreaWindow.add(MapPanel);
                ZoomAreaWindow.setVisible(true);
            }
            else //jika ingin membuat pada jendela yang sama pada peta utama
            {
                //membuat zoomArea pada tab yang disediakan
                if (this.tabPanel.indexOfTab("Zoom Area") == -1)
                {
                    panel2 = new JPanel();
                    panel2.setLayout(new BorderLayout());
                    panel2.add(this.MapPanel, BorderLayout.CENTER);
                    tabPanel.addTab("Zoom Area", panel2);
                }
                else
                {
                    JPanel temp = (JPanel)this.tabPanel.getTabComponentAt(1);
                    temp.add(this.MapPanel, BorderLayout.CENTER);
                }
            }
        }
}
